package com.honglv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.honglv.context.BaseContext;
import com.honglv.mapper.RounteMapper;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.UploadRounteDTO;

import com.honglv.pojo.entity.Rounte;
import com.honglv.pojo.vo.GetRountesByCityVO;
import com.honglv.pojo.vo.GetRountesByCityVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.RounteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class RounteServiceImpl implements RounteService {
    @Autowired
    private RounteMapper rounteMapper;
    /**
     * 用户上传路线
     * @param uploadRounteDTO
     * @return
     */
    @Override
    public void upload(UploadRounteDTO uploadRounteDTO) {
        Long authorId = BaseContext.getCurrentId();
        Rounte rounte=new Rounte();
        BeanUtils.copyProperties(uploadRounteDTO,rounte);
        rounte.setAuthorId(authorId);
        rounte.setLike(0L);
        rounte.setViews(0L);
        rounteMapper.insert(rounte);
    }
    /**
     * 根据城市分页获取路线
     * @param
     * @return
     */
    @Override
    public PageVO<GetRountesByCityVO> getRountesByCity(PageByCityDTO pageByCityDTO) {
        PageHelper.startPage(pageByCityDTO.getPage(),pageByCityDTO.getPageSize());
        QueryWrapper<Rounte> qw=new QueryWrapper<>();
        qw.like("city",pageByCityDTO.getCity());
        List<Rounte> list = rounteMapper.selectList(qw);
        GetRountesByCityVO gf=new GetRountesByCityVO();
        List<GetRountesByCityVO> rountes=new ArrayList<>();
        for(Rounte rounte:list){
            BeanUtils.copyProperties(rounte,gf);
            rountes.add(gf);
        }
        if (rountes == null){
            return null;
        }
        PageInfo<GetRountesByCityVO> pageInfo = new PageInfo<>(rountes);
        return new PageVO<>(pageInfo.getTotal(),pageInfo.getList());
    }

}
